﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Configuration;
using MailHelper;
using System.Data.SqlClient;

namespace NLCER
{
    public class NLSMain
    {
        #region Field
        static MailOper curMo;
        #endregion

        #region Function
        /// <summary>
        /// 检测并创建tsk信息
        /// </summary>
        public static void BuildTsk()
        {
            SqlHelper.ExecuteNonQuery(
                ConfigurationSettings.AppSettings["Dbconn"],
                CommandType.StoredProcedure,
                ConfigurationSettings.AppSettings["buildTskName"]);
        }

        /// <summary>
        /// 处理tsk，发送Mail
        /// </summary>
        public static void ProcessTsk()
        {
            if (null == curMo)
                curMo = new MailOper(ConfigurationSettings.AppSettings);

            string tmpNLID = "";
            string mailcontent = "";
            string corInfo = "";
            int tskcount = 30;

            StringBuilder sbtasklist = new StringBuilder();
            SqlParameter pare = new SqlParameter("@dtCount", SqlDbType.Int, 4);

            //参数值
            pare.Value = int.TryParse(ConfigurationSettings.AppSettings["tskCount"], out tskcount) ? tskcount : tskcount;

            DataTable dtTask = SqlHelper.GetDataTable(
                ConfigurationSettings.AppSettings["Dbconn"],
                CommandType.StoredProcedure,
                ConfigurationSettings.AppSettings["getTskList"],
                pare
                );

            DataTable dtNL = new DataTable();

            DataTable dtNLTem = new DataTable();

            foreach (DataRow dr in dtTask.Rows)
            {
                if (tmpNLID != dr["NLID"].ToString())
                {
                    if (tmpNLID.Length > 0)
                    {
                        //Change The NewsLetter state                       

                        SqlHelper.ExecuteNonQuery(
                            ConfigurationSettings.AppSettings["Dbconn"].ToString(),
                            CommandType.Text,
                            string.Format(ConfigurationSettings.AppSettings["chgtoProcess"], tmpNLID)
                            );
                    }
                    tmpNLID = dr["NLID"].ToString();

                    //Get NL Info
                    dtNL = SqlHelper.GetDataTable(ConfigurationSettings.AppSettings["Dbconn"].ToString(),
                        CommandType.Text,
                        string.Format(ConfigurationSettings.AppSettings["getNlInfo"], tmpNLID)
                        );


                    if (dtNL.Rows.Count > 0)
                    {
                        dtNLTem = SqlHelper.GetDataTable(ConfigurationSettings.AppSettings["Dbconn"].ToString(),
                            CommandType.Text,
                            string.Format(ConfigurationSettings.AppSettings["getNltmpInfo"], dtNL.Rows[0]["NLTeplateID"].ToString())
                            );


                        if (dtNLTem.Rows.Count > 0)
                        {

                            corInfo = ConfigurationSettings.AppSettings["conInfo"].Length > 0 ? string.Format(ConfigurationSettings.AppSettings["conInfo"], dtNL.Rows[0]["ID"]) : "";

                            //替换NLID
                            if (ConfigurationSettings.AppSettings["nlIDHolder"].ToString().Length > 0)
                            {
                                mailcontent = dtNLTem.Rows[0]["TemplateContent"].ToString().Replace(
                                    ConfigurationSettings.AppSettings["compar"],
                                    string.Format("{0}{1}",
                                        dtNL.Rows[0]["NLContent"].ToString().Replace(
                                            ConfigurationSettings.AppSettings["nlIDHolder"],
                                            dtNL.Rows[0]["ID"].ToString()),
                                        corInfo
                                        )
                                    );
                            }
                            else
                            {
                                mailcontent = dtNLTem.Rows[0]["TemplateContent"].ToString().Replace(
                                        ConfigurationSettings.AppSettings["compar"],
                                        string.Format("{0}{1}",
                                            dtNL.Rows[0]["NLContent"].ToString(),
                                            corInfo
                                            )
                                        );
                            }
                        }
                    }
                }

                if ((null != dtNL) && (dtNL.Rows.Count > 0) && (dtNLTem.Rows.Count > 0))
                {
                    curMo.SendMail(dr["Email"].ToString(),
                            ConfigurationSettings.AppSettings["SmtpFrom"],
                            dtNL.Rows[0]["Title"].ToString(),
                            mailcontent,
                            true);

                    sbtasklist.Append(string.Format("{0},", dr["ID"].ToString()));
                }
            }

            if (sbtasklist.Length > 1)
            {
                sbtasklist.Remove(sbtasklist.Length - 1, 1);
                SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings["Dbconn"].ToString(),
                CommandType.Text,
                string.Format(ConfigurationSettings.AppSettings["delTsk"], sbtasklist.ToString()));
                dtTask = null;
                dtNL = null;
            }
            tmpNLID = "";
        }
        #endregion
    }
}
